MODE_INTERACT = {}
MODE_INTERACT.HoldingObject = nil
MODE_INTERACT.HoldingObjectDistance = 0.0
MODE_INTERACT.IsRotating = false
MODE_INTERACT.MouseMoveX = 0
MODE_INTERACT.MouseMoveY = 0
MODE_INTERACT.LastAngle = Angle(0,0,0)

//****************************************************
//* Updates object carring behaviour. There is unique
//* carring behaviour for certain object types
function MODE_INTERACT:Think(player, trace)

	if not MODE_INTERACT.IsRotating then

		if MODE_INTERACT.HoldingObject and MODE_INTERACT.HoldingObject:IsValid() then

			local objectPos = player:EyePos() + (player:GetAimVector() * MODE_INTERACT.HoldingObjectDistance)

			//If we grab a track gripper, we want to rotate it about it's parent
			//This code block handles that
			if MODE_INTERACT.HoldingObject:GetClass() == "path_grip" then

				local parentNode = MODE_INTERACT.HoldingObject:GetParentNode()
				local grip = MODE_INTERACT.HoldingObject

				if parentNode:GetClass() == "animated_camera" then

					if parentNode:GetNWEntity(ANICAM_ROTGRIP) == grip then
						parentNode:SetLookAt(objectPos)
					else
						
						if parentNode:GetNWEntity(ID_PATH_DIRECTION_ENTITY_A) == trace.Entity then
							local trackTangent = objectPos - parentNode:GetPos() 
							parentNode:SetCurveTangent(trackTangent)
						else
							local trackTangent = parentNode:GetPos() - objectPos 
							parentNode:SetCurveTangent(trackTangent)
						end

					end
					
				end
				
				
			//If we grab a camera node we need to move it's grippers along with it
			//This block does that
			elseif MODE_INTERACT.HoldingObject:GetClass() == "animated_camera" then

				local parentNode = MODE_INTERACT.HoldingObject
				local grip = parentNode:GetNWEntity(ID_PATH_DIRECTION_ENTITY_A)
				local direction = grip:GetPos() - parentNode:GetPos() 
				
				local camDirGrip = parentNode:GetNWEntity(ANICAM_ROTGRIP)
				local camDirection = (camDirGrip:GetPos() - parentNode:GetPos()) 

				parentNode:SetPos(objectPos)			
				parentNode:SetCurveTangent(direction)

				parentNode:SetLookAt(camDirection + objectPos)

			else

					
				local physicsObject = MODE_INTERACT.HoldingObject:GetPhysicsObject()
				if physicsObject and physicsObject:IsValid() then 
					if physicsObject:IsMoveable() then
						MODE_INTERACT.HoldingObject:SetPos(objectPos)
					else
						MODE_INTERACT.HoldingObject = nil
					end
				else
					MODE_INTERACT.HoldingObject:SetPos(objectPos)
				end
				
			end


		end

	//This code handles object rotation
	else

	local mDX = 0.1 * MODE_INTERACT.MouseMoveX
	local mDY = 0.1 * MODE_INTERACT.MouseMoveY
	MODE_INTERACT.LastAngle = MODE_INTERACT.LastAngle + Angle(mDY, mDX, 0.0)
	
	MODE_INTERACT.HoldingObject:SetAngles( MODE_INTERACT.LastAngle)

	end
end 

function MODE_INTERACT:LeftClick(player, trace)

	if MODE_INTERACT.IsRotating then
		MODE_INTERACT.IsRotating = false
		return
	end

	if MODE_INTERACT.HoldingObject then

		local phys = MODE_INTERACT.HoldingObject:GetPhysicsObject()
		if (phys:IsValid()) then

			if MODE_INTERACT.HoldingObject:GetClass() == "path_grip" then
				phys:Sleep()
				phys:EnableMotion(false)
			else
				phys:Wake()
				if phys:IsMoveable() == false then
					phys:EnableMotion(true)
				end
			end

			phys:SetDamping(1,1)
		end

		MODE_INTERACT.HoldingObject = nil
	else

		if trace.Entity and trace.Entity:IsValid() then

			MODE_INTERACT.HoldingObjectDistance = player:EyePos():Distance(trace.Entity:GetPos())
			MODE_INTERACT.HoldingObject = trace.Entity
			local phys = MODE_INTERACT.HoldingObject:GetPhysicsObject()
			if (phys:IsValid()) then
				phys:SetDamping(1000,1000)
				phys:EnableMotion(true)
			end
		end

	end

end

function MODE_INTERACT:RightClick(player, trace)

	if MODE_INTERACT.IsRotating then
		MODE_INTERACT.IsRotating = false
	else
		local eyeAngle = player:GetAimVector()
		MODE_INTERACT.RotatingCamDir = eyeAngle:Angle()
		MODE_INTERACT.IsRotating = true
	end

end